
const router = require('koa-router')();
const uuid = require('uuid').v1;
const Multer = require("koa-multer");


let storage = Multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, "public/upload")
    },
    filename: function (req, file, cb) {
        var fileFormat = (file.originalname).split(".");
        cb(null, uuid() + "." + fileFormat[fileFormat.length - 1]);
    }
})
let upload = Multer({ storage: storage });

router.post("/api/upload", upload.fields([{ name: "file" }]), async ctx => {
    var File = [];
    for (let i = 0; i < ctx.req.files.file.length; i++) {
        const element = ctx.req.files.file[i];
        File.push('/upload/' + element.filename);
    }
    ctx.body = File;
})

module.exports = router